package com.aaa.phone.service.appuserwalletservice.impl;

import com.aaa.entity.AppUserWallet;
import com.aaa.phone.dao.appuserwalletdao.IAppUserWalletDao;
import com.aaa.phone.service.appuserwalletservice.AppUserWalletService;
import com.aaa.phone.systemconst.ISystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 用户钱包信息service实现类
 * @Author: 陈阳
 * @CreateDate: 2018/12/21$ 20:43$
 */
@Transactional
@Service
public class AppUserWalletServiceImpl implements AppUserWalletService,ISystemConst {
    @Autowired
    private IAppUserWalletDao dao;

    /**
     * 获取用户余额
     */
    @Override
    public double getDefference(String telephone) {
        double defference = dao.getDefference(telephone);
        if(defference>= ZERO){
            return defference;
        }
        return ZERO;
    }

    @Override
    public int toRecharge(String telephone, double money) {
        return dao.toRecharge(telephone,money);
    }

    /**
     * 用户充值
     * */
    @Override
    public double updateRecharge(String telephone, double money) {
        //为用户进行充值
        int toRecharge = dao.toRecharge(telephone, money);
        //在余额表添加充值金额
        int updateRecharge = dao.updateRecharge(telephone, money);
        double defference = getDefference(telephone);
        if(toRecharge>ZERO && updateRecharge>ZERO){
            //如果充值并且到账返回账号余额
            return defference;
        }
        //失败返回失败码
        return ERROR_CODE;
    }

    @Override
    public int toEnchashment(String telephone, double money) {
        return dao.toEnchashment(telephone,money);
    }

    /**
     * 用户提现
     * */
    @Override
    public double updateEnchashment(String telephone, double money) {
        //为用户提现
        int toEnchashment = dao.toEnchashment(telephone, money);
        //在余额表减少相应的余额
        int updateEnchashment = dao.updateEnchashment(telephone, money);
        double defference = getDefference(telephone);
        if(updateEnchashment>ZERO && toEnchashment>ZERO){
            //如果提现成功则返回用户余额
            return defference;
        }
        //提现失败则返回失败码
        return ERROR_CODE;
    }

    /**
     * 获取用户充值提现记录
     * */
    @Override
    public List<AppUserWallet> getRechargeEnchashment(String telephone) {
        return dao.getRechargeEnchashment(telephone);
    }


}
